﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Islamic_influence_in_Venetian_cuture.LangResources;
using System.Collections.ObjectModel;

namespace Islamic_influence_in_Venetian_cuture.View.UserControls
{
    public partial class InfluenceMenuPivotElement : UserControl
    {
        private int _index;
        public InfluenceMenuPivotElement(int index)
        {
            var lang = new Lang();
            DataContext = lang;
            InitializeComponent();

            var d_c = SuperString.CreateCollection(lang, index);
            Arguments.DataContext = d_c;
            _index = index;
        }

        private void Arguments_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Arguments.SelectedItem == null) return;
            var context = Arguments.SelectedItem as SuperString;
            InfluenceKind.KindIndex = context.Idx;
            if (SelectionChanged != null)
                SelectionChanged(sender, e);
            //NavigationService.Navigate(new Uri("/View/InfluenceContent.xaml", UriKind.Relative));
        }

        public event SelectionChangedEventHandler SelectionChanged;
    }

    public class SuperString : INotifyPropertyChanged
    {
        private string _name;
        private Lang _lang;

        public Cultures Culture { get { return Lang.CurrentCulture; } }

        private string _idx;
        public string Idx
        {
            get { return _idx; }
        }
        

        public string _value;

        public string TheValue
        {
            get { return _value; }
        }

        public SuperString(Lang lang, string name)
        {
            _name = name;
            _lang = lang;
            lang.PropertyChanged += new PropertyChangedEventHandler(lang_PropertyChanged);
            var props = typeof(LangResources.Lang).GetProperties().Where(p => p.Name == _name).First();
            var names = props.GetGetMethod().Invoke(_lang, null) as string;
            _value = names;
            _idx = name.Substring(name.Length - 2);
        }

        private void lang_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _value = typeof(LangResources.Lang).GetProperties().Where(p => p.Name == _name).First().GetGetMethod().Invoke(_lang, null) as string;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(""));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return _value;
        }

        public static ObservableCollection<SuperString> CreateCollection(Lang lang , int index)
        {
            var ret = new ObservableCollection<SuperString>();
            var names = typeof(Lang).GetProperties().Where(p => p.Name.StartsWith("ArtContent" + index)).Select(name => new SuperString(lang, name.Name)).ToObservableCollection();
            //foreach (var name in names)
            //{
            //    ret.Add(new SuperString(lang, name.Name));
            //}
            return names;
        }
    }
}
